# -*- coding: utf-8 -*-
# @Time    : 2025/10/9
# @Author  : Qwen
# @File    : baidu_map_controller.py
# @Describe: 百度地图路线规划接口控制器

from flask import Blueprint, request, jsonify, current_app
from luck.map.sdk.baidu.service.MapSdkBaiduService import MapSdkBaiduService
from luck.core.LuckConstant import LuckConstant
from luck.util.LuckLogHelperUtils import LuckLogHelperUtils

# 创建蓝图
baidu_map_bp = Blueprint('baidu_map_bp', __name__, url_prefix='/map/sdk/baidu/mapSdkBaidu')

@baidu_map_bp.route('/noGetwayReadRoute', methods=['POST'])
def get_baidu_route():
    """
    获取百度地图路线规划信息
    请求参数:
    - origin: 起点坐标 (纬度,经度)
    - destination: 终点坐标 (纬度,经度)
    - route_type: 路线类型 (driving, walking, riding)
    - tactics: 路线偏好
    - ak: 百度地图API Key (必需)
    """
    try:
        # 获取请求参数
        data = request.get_json()
        if not data:
            return jsonify({
                "code": LuckConstant.CODE_FAIL,
                "msg": "请求参数不能为空"
            }), 400
            
        origin = data.get('origin')
        destination = data.get('destination')
        route_type = data.get('route_type', 'driving')
        tactics = data.get('tactics', '0')  # 获取路线偏好参数
        ak = data.get('ak')  # 获取API Key参数
        
        # 参数验证
        if not origin or not destination:
            return jsonify({
                "code": LuckConstant.CODE_FAIL,
                "msg": "起点和终点不能为空"
            }), 400
            
        # 验证API Key
        if not ak:
            return jsonify({
                "code": LuckConstant.CODE_FAIL,
                "msg": "百度地图API Key不能为空"
            }), 400
            
        # 记录请求日志
        config_data = current_app.config.get('CONFIG_DATA', {})
        log = LuckLogHelperUtils(config_data)
        log.add_visit_log({
            "requri": request.path,
            "reqtype": request.mimetype,
            "reqmethod": request.method,
            "reqparam": str(data),
            "reqnote": "百度地图路线规划接口请求"
        })
        
        # 创建百度地图服务实例并传递API Key
        map_service = MapSdkBaiduService(ak)
        
        # 调用服务获取路线信息
        result = map_service.get_route_info(origin, destination, route_type, tactics)
        
        if result["code"] == LuckConstant.CODE_SUCCESS:
            # 记录成功响应日志
            log.add_visit_log({
                "requri": request.path,
                "reqtype": request.mimetype,
                "reqmethod": request.method,
                "reqparam": str(data),
                "reqnote": "百度地图路线规划接口响应成功"
            })
            return jsonify({
                "code": LuckConstant.CODE_SUCCESS,
                "msg": LuckConstant.MSG_SUCCESS,
                "data": result["data"]
            }), 200
        else:
            # 记录失败响应日志
            log.add_error_log({
                "requri": request.path,
                "reqtype": request.mimetype,
                "reqmethod": request.method,
                "reqparam": str(data),
                "reqnote": "百度地图路线规划接口响应失败",
                "reqerror": result["msg"]
            })
            return jsonify({
                "code": LuckConstant.CODE_FAIL,
                "msg": result["msg"]
            }), 500
            
    except Exception as e:
        # 记录错误日志
        config_data = current_app.config.get('CONFIG_DATA', {})
        log = LuckLogHelperUtils(config_data)
        log.add_error_log({
            "requri": request.path,
            "reqtype": request.mimetype,
            "reqmethod": request.method,
            "reqparam": str(request.get_json() if request.is_json else {}),
            "reqnote": "百度地图路线规划接口异常",
            "reqerror": str(e)
        })
        
        return jsonify({
            "code": LuckConstant.CODE_FAIL,
            "msg": f"服务内部错误: {str(e)}"
        }), 500